const   router = require('koa-router')(),
        user = require('./admin/user.js'),
        focus = require('./admin/focus.js'),
        newscate = require('./admin/newscate.js'),
        login = require('./admin/login.js'),
        manage = require('./admin/manage.js'),
        index = require('./admin/index.js'),
        articlecate = require('./admin/articlecate.js'),
        article = require('./admin/article.js'),
        url = require('url')

/* 配置中间件获取路径 */
router.use(async(ctx,next) =>{
    // console.log(ctx.request.header.host)
    ctx.state.__HOST__ = 'http://' + ctx.request.header.host

    var pathname = url.parse(ctx.request.url).pathname
    var pathname1 = url.parse(ctx.request.url).pathname.substring(1)

    var suplitUrl = pathname1.split('/')
    /* 全局的userInfo */
    ctx.state.G = {
        url:suplitUrl,
        userInfo:ctx.session.userInfo,
        prevPage:ctx.request.headers['referer']
    }

/* 权限判断 */
    if(ctx.session.userInfo){
        await next()
    }else{
        //是否是登陆页面
        if(pathname == '/admin/login' || pathname == '/admin/login/doLogin' || pathname == '/admin/login/code'){
            await next()
        }else{
            ctx.redirect('/admin/login')
        }
     
    }
})



router.get('/',async (ctx) => {
   await ctx.render('admin/index')
})
router.use(index)
router.use('/user',user)
router.use('/focus',focus)
router.use('/newscate',newscate)
router.use('/login',login)
router.use('/manage',manage)
router.use('/articlecate',articlecate)
router.use('/article',article)

module.exports = router.routes()